package com.ruoyi.function.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.function.domain.ServiceArea;
import com.ruoyi.function.domain.vo.ServiceAreaVO;
import com.ruoyi.function.service.IServiceAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//服务区域相关接口
@RestController
@RequestMapping("/api/service/area")
public class ServiceAreaController extends BaseController {
    
    @Autowired
    private IServiceAreaService serviceAreaService;
    
    /**
     * 获取省份列表
     */
    @Anonymous
    @GetMapping("/provinces")
    public AjaxResult getProvinces() {
        return AjaxResult.success(serviceAreaService.getAreasByLevel(1));
    }
    
    /**
     * 获取下级区域
     */
    @Anonymous
    @GetMapping("/children/{parentCode}")
    public AjaxResult getChildren(@PathVariable String parentCode) {
        return AjaxResult.success(serviceAreaService.getChildrenByParentCode(parentCode));
    }

    /**
     * 获取区域树状数据
     */
    @Anonymous
    @GetMapping("/tree")
    public AjaxResult getAreaTree() {
        List<ServiceAreaVO> tree = serviceAreaService.getAreaTree();
        return AjaxResult.success(tree);
    }
    
    /**
     * 根据层级获取区域列表
     */
    @Anonymous
    @GetMapping("/level/{level}")
    public AjaxResult getAreasByLevel(@PathVariable Integer level) {
        List<ServiceAreaVO> areas = serviceAreaService.getAreasByLevel(level);
        return AjaxResult.success(areas);
    }

    /**
     * 根据区域编码获取区域信息
     */
    @Anonymous
    @GetMapping("/code/{areaCode}")
    public AjaxResult getAreaByCode(@PathVariable String areaCode) {
        ServiceArea area = serviceAreaService.getAreaByCode(areaCode);
        return AjaxResult.success(area);
    }
    
    /**
     * 获取区域完整路径名称
     */
    @Anonymous
    @GetMapping("/fullpath/{areaCode}")
    public AjaxResult getAreaFullPath(@PathVariable String areaCode) {
        String fullPath = serviceAreaService.getAreaFullPathName(areaCode);
        return AjaxResult.success(fullPath);
    }
    
    /**
     * 批量获取区域完整路径名称
     */
    @Anonymous
    @PostMapping("/fullpaths")
    public AjaxResult getAreaFullPaths(@RequestBody List<String> areaCodes) {
        Map<String, String> fullPaths = serviceAreaService.getAreaFullPathNames(areaCodes);
        return AjaxResult.success(fullPaths);
    }
} 